<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="工单编码" prop="workorderCode">
        <el-input v-model="queryParams.workorderCode" placeholder="请输入工单编码" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="SN码" prop="snCode">
        <el-input v-model="queryParams.snCode" placeholder="请输入SN码" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="变更开始时间" prop="beginChangeDate">
        <el-date-picker value-format="yyyy-MM-dd" v-model="queryParams.beginChangeDate" type="date" placeholder="选择变更开始时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="变更结束时间" prop="endChangeDate">
        <el-date-picker value-format="yyyy-MM-dd" v-model="queryParams.endChangeDate" type="date" placeholder="选择变更结束时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport" v-hasPermi="['pro:changebom:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="detailsList">
      <el-table-column label="工单编码" align="center" prop="workorderCode" />
      <el-table-column label="单据日期" align="center" prop="docDate" />
      <el-table-column label="类型" align="center" prop="orderType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.mes_order_type" :value="scope.row.orderType" />
        </template>
      </el-table-column>
      <el-table-column label="流程状态" align="center" prop="workflowStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.workflow_status" :value="scope.row.workflowStatus" />
        </template>
      </el-table-column>
      <el-table-column label="产品编号" align="center" prop="productCode" />
      <el-table-column label="产品名称" align="center" prop="productName" />
      <el-table-column label="规格型号" align="center" prop="productSpc" />
      <el-table-column label="单位" align="center" prop="unitName" />
      <el-table-column label="工单数量" align="center" prop="quantity" />
      <el-table-column label="原版本号" align="center" prop="oldVersion" />
      <el-table-column label="新版本号" align="center" prop="newVersion" />
      <el-table-column label="变更人" align="center" prop="createBy" />
      <el-table-column label="变更时间" align="center" prop="createTime" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="showDetail(scope.row)">详情</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList" />

    <!-- 添加或修改用料清单变更明细对话框 -->
    <el-drawer :title="title" :visible.sync="open" size="80%" append-to-body>
      <div style="display: flex">
        <div style="flex: 0 0 50%;padding: 0 12px;">
          <div style="font-size:16px;font-weight:bold;margin-bottom:10px;padding-left:10px;">原版本用料清单：{{ detailRow.oldVersion}} </div>
          <el-table v-loading="loading" :data="oldList" border height="500">
            <el-table-column type="index" label="序号" align="center" width="50" />
            <el-table-column label="物料编号" align="center" prop="itemCode" />
            <el-table-column label="物料名称" align="center" prop="itemName" />
            <el-table-column label="规格型号" align="center" prop="specification" />
            <el-table-column label="数量" align="center" prop="fmustQty" />
          </el-table>
        </div>
        <div style="flex:  0 0 50%;padding:0 12px;">
          <div style="font-size:16px;font-weight:bold;margin-bottom:10px;padding-left:10px;">新版本用料清单：{{ detailRow.newVersion}} </div>
          <el-table v-loading="loading" :data="newList" border height="500">
            <el-table-column type="index" label="序号" align="center" width="50" />
            <el-table-column label="物料编号" align="center" prop="itemCode" />
            <el-table-column label="物料名称" align="center" prop="itemName" />
            <el-table-column label="规格型号" align="center" prop="specification" />
            <el-table-column label="数量" align="center" prop="fmustQty" />
          </el-table>
        </div>
      </div>
      <div style="padding:0 12px;font-size:16px;font-weight:bold;margin-bottom:4px;margin-top:26px;padding-left:24px;">清单比对结果</div>
      <div style="padding:0 12px;">
        <el-table v-loading="loading" :data="compareList" border>
          <el-table-column type="index" label="序号" align="center" width="50" />
          <el-table-column label="物料编号" align="center" prop="itemCode" />
          <el-table-column label="物料名称" align="center" prop="itemName" />
          <el-table-column label="规格型号" align="center" prop="specification" />
          <el-table-column label="类型" align="center" prop="compareType">
            <template slot-scope="scope">
              <span>{{scope.row.compareType === 1? '退料': '增料'}}</span>
            </template>
          </el-table-column>
          <el-table-column label="数量" align="center" prop="fmustQty" />
        </el-table>
      </div>
    </el-drawer>
  </div>
</template>

<script>
import {
  listDetails,
  selectMdItemsForWorkorderBom,
  bomCompare,
  listExport
} from "@/api/mes/pro/bom/changedetails.js";

export default {
  name: "Changebom",
  dicts: [
    "workflow_status",
    "mes_order_type",
  ],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 用料清单变更明细表格数据
      detailsList: [],
      // 弹出层标题
      title: "用料清单详情",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        workorderCode: null,
        beginChangeDate: null,
        endChangeDate: null,
        snCode: null,
      },
      oldList: [],
      newList: [],
      compareList: [],
      detailRow: {},
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询用料清单变更明细列表 */
    getList() {
      this.loading = true;
      listDetails(this.queryParams).then((response) => {
        this.detailsList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        workorderId: null,
        workorderCode: null,
        oldFbomId: null,
        oldVersion: null,
        newFbomId: null,
        newVersion: null,
        createBy: null,
        createTime: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 查看详情 */
    showDetail(row) {
      this.reset();
      this.open = true;
      this.detailRow = row
      const oldParam = {
        // fbomId: row.oldFbomId,
        version: row.oldVersion,
        workorderId: row.workorderId,
        fMOEntryID: row.fmoEntryid
      };
      const newParam = {
        // fbomId: row.newFbomId,
        version: row.newVersion,
        workorderId: row.workorderId,
        fMOEntryID: row.fmoEntryid
      };
      Promise.all([
        selectMdItemsForWorkorderBom(oldParam),
        selectMdItemsForWorkorderBom(newParam),
      ]).then((res) => {
        this.oldList = res[0].bomItems || []
        this.newList = res[1].bomItems || []
        const param = {
          oldBomItems: this.oldList,
          newBomItems: this.newList
        }
        bomCompare(param).then(res2 => {
          this.compareList = res2.data || []
        })
      });
      // getDetails(id).then(response => {
      //   this.form = response.data;
      //   this.open = true;
      // });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "/mes/pro/workorder/bom/changedetails/export",
        {
          ...this.queryParams,
        },
        `details_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
